package com.ruoyi.am.controller;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.framework.web.domain.server.Sys;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.am.domain.AmTrip;
import com.ruoyi.am.service.IAmTripService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 长途信息Controller
 * 
 * @author hkkk
 * @date 2020-11-30
 */
@Controller
@RequestMapping("/am/trip")
public class AmTripController extends BaseController
{
    private String prefix = "am/trip";

    @Autowired
    private IAmTripService amTripService;

    @RequiresPermissions("am:trip:view")
    @GetMapping()
    public String trip()
    {
        return prefix + "/trip";
    }

    /**
     * 查询长途信息列表
     */
    @RequiresPermissions("am:trip:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmTrip amTrip)
    {
        startPage();
        List<AmTrip> list = amTripService.selectAmTripList(amTrip);
        return getDataTable(list);
    }

    /**
     * 导出长途信息列表
     */
    @RequiresPermissions("am:trip:export")
    @Log(title = "长途信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmTrip amTrip)
    {
        List<AmTrip> list = amTripService.selectAmTripList(amTrip);
        ExcelUtil<AmTrip> util = new ExcelUtil<AmTrip>(AmTrip.class);
        return util.exportExcel(list, "trip");
    }

    /**
     * 新增长途信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存长途信息
     */
    @RequiresPermissions("am:trip:add")
    @Log(title = "长途信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmTrip amTrip)
    {
        return toAjax(amTripService.insertAmTrip(amTrip));
    }

    /**
     * 修改长途信息
     */
    @GetMapping("/edit/{tripId}")
    public String edit(@PathVariable("tripId") Long tripId, ModelMap mmap)
    {
        AmTrip amTrip = amTripService.selectAmTripById(tripId);
        mmap.put("amTrip", amTrip);
        return prefix + "/edit";
    }

    /**
     * 修改保存长途信息
     */
    @RequiresPermissions("am:trip:edit")
    @Log(title = "长途信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmTrip amTrip)
    {
        return toAjax(amTripService.updateAmTrip(amTrip));
    }

    /**
     * 删除长途信息
     */
    @RequiresPermissions("am:trip:remove")
    @Log(title = "长途信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amTripService.deleteAmTripByIds(ids));
    }

    @ResponseBody
    @PostMapping("/addTrip")
    public AjaxResult addTrip(@RequestParam String obj_data){
        System.out.println(obj_data);
        JSONObject object=JSONObject.parseObject(obj_data);
        AmTrip amTrip= JSON.toJavaObject(object,AmTrip.class);
        amTrip.setBeginTime(new Date(object.getString("beginTimes")));
        amTrip.setEndTime(new Date(object.getString("endTimes")));
        return toAjax(amTripService.insertAmTrip(amTrip));
    }
}
